package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.service.ReportService;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = ReportService.class)
@Transactional
public class ReportServiceImpl implements ReportService {
    @Autowired
    private MemberDao memberDao;
    @Autowired
    private OrderDao orderDao;

    //统计运营数据,报表
    public Map<String, Object> getBusinessReportData() {
        try {
            //获得当天日期->报表日期
            String today = DateUtils.parseDate2String(DateUtils.getToday());
            //获得本周一的日期
            String thisWeekMonday = DateUtils.parseDate2String(DateUtils.getThisWeekMonday());
            //获得本月第一天日期 2020-08-01
            String firstDay4ThisMonth = DateUtils.parseDate2String(DateUtils.getFirstDay4ThisMonth());

            //当日会员新增数
            Integer todayNewMember = memberDao.findMemberCountByDate(today);
            //会员总数
            Integer totalMember = memberDao.findMemberTotalCount();
            //本周会员新增数
            Integer thisWeekNewMember = memberDao.findMemberCountAfterDate(thisWeekMonday);
            //当月会员新增数
            Integer thisMonthNewMenber = memberDao.findMemberCountAfterDate(firstDay4ThisMonth);

            //预约今日数
            Integer todayOrderNumber = orderDao.findOrderCountByDate(today);
            //本周预约数
            Integer thisWeekOrderNumber = orderDao.findOrderCountByFirstAndEndDate(thisWeekMonday, today);
            //本月预约数
            Integer thisMonthOrderNumber = orderDao.findOrderCountByFirstAndEndDate(firstDay4ThisMonth, today);

            //今日到诊数
            Integer todayVisitsNumber = orderDao.findVisitsCountByDate(today);
            //本周到诊数
            Integer thisWeekVisitsNumber = orderDao.findVisitsCountAfterDate(thisWeekMonday);
            //本月到诊数
            Integer thisMonthVisitsNumber = orderDao.findVisitsCountAfterDate(firstDay4ThisMonth);

            //热门套餐查询
            List<Map> hotSetmeal = orderDao.findHotSetmeal();

            //封装数据Map集合
            Map<String, Object> resultDate = new HashMap<>();

            resultDate.put("reportDate", today);
            resultDate.put("totalMember", totalMember);

            resultDate.put("todayNewMember", todayNewMember);
            resultDate.put("thisWeekNewMember", thisWeekNewMember);
            resultDate.put("thisMonthNewMember", thisMonthNewMenber);

            resultDate.put("todayOrderNumber", todayOrderNumber);
            resultDate.put("thisWeekOrderNumber", thisWeekOrderNumber);
            resultDate.put("thisMonthOrderNumber", thisMonthOrderNumber);

            resultDate.put("todayVisitsNumber", todayVisitsNumber);
            resultDate.put("thisWeekVisitsNumber", thisWeekVisitsNumber);
            resultDate.put("thisMonthVisitsNumber", thisMonthVisitsNumber);

            resultDate.put("hotSetmeal", hotSetmeal);

            return resultDate;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }
}
